# -*- coding: utf-8 -*-

from lib.ucloud.ufile import getufilelist
from src.ucloud.ufile_config import read_config
from src.db.ucloud_db import insert_item
from twisted.internet import reactor
from src.sshconn.ssh_conn import singleton
from multiprocessing.dummy import Pool as ThreadPool
from lib.ucloud.logger import logger
# import asyncio


@singleton
class ReadLogList(object):

    def __init__(self):
        public_key, private_key, bucket = read_config()

        self.bucket = bucket
        # 构造对象，并设置公私钥
        self.handler = getufilelist.GetFileList(public_key, private_key)

        # 设置每页的数量
        self.limit = 500
        self.pool = ThreadPool(500)  # 1000是线程池中线程的个数

    def get_next_page(self):
        ret_one, resp = self.handler.getfilelist(self.bucket, limit=self.limit)
        self.pool.map(self.insert_page_item, ret_one['DataSet'])

        while True:
            if ret_one['NextMarker'] != "":
                ret, resp = self.handler.getfilelist(self.bucket, limit=self.limit, marker=ret_one['NextMarker'])
                ret_one = ret
                self.pool.map(self.insert_page_item, ret_one['DataSet'])
            else:
                break
        logger.info("insert end")
        # reactor.stop()

    def insert_page_item(self, item):
        insert_item(item)


if __name__ == "__main__":
    reactor.callWhenRunning(ReadLogList().get_next_page)
    reactor.run()
